<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <base href="<%=basePath%>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <title>权限</title>
    <%@include file="../../common.jsp"%>
</head>
<body>
    <div class="bigBox">
        <h2>权限列表</h2>
        <hr>
        <table class="layui-table" id="permissionTable" lay-filter="permissionTable"></table>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="add" onclick="addParent()"><i class="layui-icon layui-icon-add-circle"></i>添加一级菜单</button>
                <button class="layui-btn layui-btn-sm" lay-event="export"><i class="layui-icon layui-icon-export"></i> 导出数据</button>
            </div>
        </script>

        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add" href="javascript:void(0)">添加</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" href="javascript:void(0)">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" href="javascript:void(0)">删除</a>
        </script>

        <script type="text/javascript">
            var treeGrid = null;
            //引用treeGrid
            layui.config({
                base : '/layui/lay/modules/'
            }).extend({
                treeGrid : 'treeGrid'
            })

            function render() {
                layui.use(['table','treeGrid','layer','form'],function () {
                    treeGrid = layui.treeGrid;
                    var table = layui.table;

                    //获取数据,渲染treeGrid
                    $.get('/function/select',function (d) {
                        $.each(d.data,function () {
                            this.isOpen = true
                        });
                        treeGrid.render({
                            id : 'permissionTable',
                            elem : '#permissionTable',
                            data : d.data,
                            idField : 'id',
                            treeId : 'id',
                            cellMinWidth: 150,
                            treeUpId : 'parentId',
                            treeShowName : 'functionName',
                            toolbar : '#toolbarDemo',
                            cols : [[
                                {field:'functionName', title:'功能名称'},
                                {field:'functionCode', title:'功能编码'},
                                {field:'createdBy', title:'创建人'},
                                {field:'funUrl', title:'链接地址'},
                                {field:'isStart',title:'是否启用',templet:function (d) {
                                        var str = d.isStart == 1 ? "checked" : "";
                                        var check = '<input type="checkbox" title="启用" '+str+' name="isStart">';
                                        return check;
                                    }},
                                {toolbar:'#barDemo',title:'操作'}
                            ]]
                        });
                    })

                    treeGrid.on('tool(permissionTable)',function (obj) {
                        if(obj.event === 'add'){
                            $.get('${path}/function/addParent',function (view) {
                                layer.open({
                                    type:1,
                                    title:'添加权限',
                                    content: view,
                                    area:'700px',
                                    skin:'layui-layer-molv',
                                    btn:['确定','取消'],
                                    success:function () {
                                        //渲染下拉框值
                                        $.get("function/selectParent",function(da){
                                            $.each(da,function(){
                                                var opt = $("<option></option>").appendTo("#function");
                                                opt.text(this.functionName).val(this.id);
                                            });
                                        });
                                        layui.form.render();

                                        $.get('function/get',{id:obj.data.id},function (data) {
                                            layui.form.val("functionForm",{
                                                'parentId':data.id
                                            });
                                        })
                                        layui.form.render();
                                        $(":input[name = 'parentId']").attr("disabled",true);
                                    },
                                    yes:function (index) {

                                        $(":input[name = 'parentId']").attr("disabled",false);
                                        var params = $("#functionForm").serialize();
                                        $.post('/function/addParent',params,function (data) {
                                            if(data){
                                                layer.close(index);
                                                layer.msg('添加成功!',{icon:1})
                                                render();//重载treeGrid
                                            }else{
                                                layer.msg('添加成功!',{icon:5})
                                            }
                                        })
                                    }
                                })
                            });
                        }else if(obj.event === 'edit'){
                            $.get('${path}/function/addParent',function (view) {
                                layer.open({
                                    type:1,
                                    title:'修改权限',
                                    content: view,
                                    area:'700px',
                                    skin:'layui-layer-molv',
                                    btn:['确定','取消'],
                                    success:function () {
                                        $.get('function/get',{id:obj.data.id},function (data) {

                                            var parentIds = new Array();//所有需要清除的权限
                                            var functions = obj.data;
                                            console.log(functions);
                                            console.log(functions.children.length);
                                            for(let psfunction of data.children) {
                                                parentIds.push(psfunction.id);
                                            }

                                            $.get("function/selectParent",function(da){
                                                $.each(da,function(){
                                                    if(this.functionName != data.functionName){
                                                        if($.inArray(this.id,parentIds) == -1){
                                                            var opt = $("<option></option>").appendTo("#function");
                                                            opt.text(this.functionName).val(this.id);
                                                        }
                                                    }
                                                });

                                                layui.form.render();
                                                layui.form.val("functionForm",{
                                                    'parentId':data.parentId
                                                });
                                                layui.form.render();
                                            });

                                            layui.form.val('functionForm',data);
                                            $(":input[name = 'parentId']").attr("disabled",true);
                                        })

                                    },
                                    yes:function (index) {
                                        var params = $("#functionForm").serialize();
                                        $.post('/function/update',params,function (data) {
                                            if(data){
                                                layer.close(index);
                                                layer.msg('修改成功!',{icon:1})
                                                render();//重载treeGrid
                                            }else{
                                                alert("修改失败!");
                                            }
                                        })
                                    }
                                })
                            });
                        }else if(obj.event === 'del'){
                            //查询需要删除的权限是否有子权限
                            $.get("function/getFunction",{id:obj.data.id},function (data) {
                                if(data.length != 0){
                                    layer.msg('请先删除子权限!!!')
                                }else{
                                    $.get("function/selectRoleByFunId",{funId:obj.data.id},function (data) {
                                        if(data != 0){
                                            layer.confirm('当前权限仍有角色在使用,确定要删除?',{icon:3,title:'系统提示'},function (index) {
                                                //执行删除
                                                $.get("function/delete",{id:obj.data.id},function (data) {
                                                    if(data){
                                                        layer.close(index);
                                                        layer.msg('删除成功!',{icon:1})
                                                        render();//重载treeGrid
                                                    }
                                                })
                                            });
                                        }else{
                                            layer.confirm('确定要删除?',{icon:3,title:'系统提示'},function (index) {
                                                //执行删除
                                                $.get("function/delete",{id:obj.data.id},function (data) {
                                                    if(data){
                                                        layer.close(index);
                                                        layer.msg('删除成功!',{icon:1})
                                                        render();//重载treeGrid
                                                    }
                                                })
                                            });
                                        }
                                    })

                                }
                            })
                        }
                    })
                });
            }

            //渲染treeGrid
            render();

            //添加功能
            function addParent(){
                $.get('${path}/function/addParent',function (view) {
                    layer.open({
                        type:1,
                        title:'添加权限',
                        content: view,
                        area:'700px',
                        skin:'layui-layer-molv',
                        btn:['确定','取消'],
                        success:function () {
                            $(":input[name = 'parentId']").attr("disabled","disabled");
                        },
                        yes:function (index) {
                            var params = $("#functionForm").serialize();
                            $.post('/function/addParent',params,function (data) {
                                if(data){
                                    layer.close(index);
                                    layer.msg('添加成功!',{icon:1})
                                    render();//重载treeGrid
                                }else{
                                    alert("添加失败!");
                                }
                            })
                        }
                    })
                });

            }


        </script>
    </div>
</body>
</html>